import { login, logout, getInfo } from '@/api/user'
// import { logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import service from '@/utils/request'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    console.log(username, password)
    return new Promise((resolve, reject) => {
      /* 原本 */
      // login({ username: username.trim(), password: password }).then(response => {
      //   console.log(response, 'ppp')
      //   // const { data } = response
      //   const token = response.token
      //   commit('SET_TOKEN', token)
      //   setToken(token)
      //   resolve()
      // }).catch(error => {
      //   reject(error)
      // })
      /* now */
      service
        .post('/login', {
          username: username,
          password: password,
          loginType: '0'
        })
        .then((res) => {
          const token = res.data.token
          window.localStorage.setItem('username', res.data.info.username)
          commit('SET_TOKEN', token)
          setToken(token)
          resolve()
        })
        .catch((err) => {
          reject(err)
        })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      /* 原本 */
      // getInfo(state.token)
      //   .then((response) => {
      //     console.log(response, "ooppii");
      //     const { data } = response;
      //     if (!data) {
      //       return reject("Verification failed, please Login again.");
      //     }
      //     const { name, avatar } = data;
      //     commit("SET_NAME", name);

      //     commit("SET_AVATAR", avatar);
      //     resolve(data);
      //   })
      //   .catch((error) => {
      //     reject(error);
      //   });

      /* now */
      service
        .get('/getUserInfo', {
          params: {
            username: window.localStorage.getItem('username')
          }
        })
        .then((res) => {
          if (res.code == 200) {
            commit('SET_NAME', res.data.username)
            commit('SET_AVATAR', res.data.url)
            resolve()
          } else {
            // this.$message.error('错了哦，这是一条错误消息')
          }
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // logoout 退出登录接口
      // 退出登录不需要调接口
      // logout(state.token).then(() => {
      removeToken() // must remove  token  first  删除token
      resetRouter() // 重置路由
      commit('SET_TOKEN', '')

      commit('RESET_STATE')
      resolve()
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
